﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using SN.Data;
using Agro.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Ninject;
using Agro.Services;
using Agro.Library;
namespace Agro.Web.Controllers
{
    public class HomeController : BaseController
    {

        [Inject]
        public IMenuService MenuService { get; set; }
        [Inject]
        public IMessageService MessageService { get; set; }
        [Inject]
        public INoticeService NoticeService {get;set; }
        [Inject]
        public IFileService FileService { get; set; }
        [Inject]
        public IBbsService BbsService { get; set; }
        [Inject]
        public IOrganizationService OrganizationService { get; set; }
        public ActionResult Index()
        {

            DataTable dtMenu = MenuService.GetUserMenuIdDT(CurrUser);
            List<T_Menu> list = new List<T_Menu>();
            var q = from c in dtMenu.AsEnumerable()
                    where c.Field<int>("Menu_ParentID") == 0
                    orderby c.Field<int>("Menu_Order")
                    select new T_Menu { Menu_ID = c.Field<int>("Menu_ID"), Menu_NAME = c.Field<string>("Menu_NAME"), Menu_Url = c.Field<string>("Menu_Url") };
            list.AddRange(q);
            ViewBag.MenuList = list;
            T_Organization organization = MenuService.GetEntity<T_Organization>(T_Organization._.Organization_ID == CurrUser.UserInfo.User_OrganizationID);
            ViewBag.Organization_Name = organization.Organization_Name;//在前台将所属单位也显示
            //获取消息内容 

            ViewBag.Message = MessageService.GetUserReadMessage(CurrUser);
            ViewBag.OnlineUserNum = HttpContext.Application["user_sessions"].ToString(); //获取在线人数

            return View(CurrUser.UserInfo);
        }

        public ActionResult TreeGetList(string id)
        {
            DataTable dataTable = MenuService.TreeGetList(CurrUser, id);
            DataColumn dc = new DataColumn("open", typeof(Int32));
            dc.DefaultValue = 1;
            dataTable.Columns.Add(dc);
            string result = JsonConvert.SerializeObject(dataTable, new DataTableConverter());
            return Content(result);
        }
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Default()
        {
           // Organization_Type

            int GlCount = OrganizationService.GetCountOrgByType(CurrUser.UserInfo.User_OrganizationID,"0");
            ViewBag.GlCount = GlCount;
            //管理部上报情况
            DataTable dtCXJG = OrganizationService.GetCXJG(CurrUser.UserInfo.User_OrganizationID);

            string a1 = dtCXJG.Compute("Count(bmbh)", "state=0").ToString();//未上报的数量
            string a2 = dtCXJG.Compute("Count(bmbh)", "state=1").ToString();//上报未完成的数量
            string a3 = dtCXJG.Compute("Count(bmbh)", "state=2").ToString();//上报完成的数量

            ViewBag.a1 = a1;
            ViewBag.a2 = a2;
            ViewBag.a3 = a3;
            ViewBag.CXJG = dtCXJG;
 //龙头企业上报情况
            DataTable dtQYCXJG = OrganizationService.GetQYCXJG(CurrUser.UserInfo.User_OrganizationID);

            string b1 = dtQYCXJG.Compute("Count(bmbh)", "state=0").ToString();//未上报的数量
            string b2 = dtQYCXJG.Compute("Count(bmbh)", "state=1").ToString();//上报未完成的数量
            string b3 = dtQYCXJG.Compute("Count(bmbh)", "state=2").ToString();//上报完成的数量

            ViewBag.b1 = b1;
            ViewBag.b2 = b2;
            ViewBag.b3 = b3;
            ViewBag.QYCXJG = dtQYCXJG;

            int QyCount = OrganizationService.GetCountOrgByType(CurrUser.UserInfo.User_OrganizationID,"1");
            ViewBag.QyCount = QyCount;

            T_Organization organization = MenuService.GetEntity<T_Organization>(T_Organization._.Organization_ID == CurrUser.UserInfo.User_OrganizationID);
            ViewBag.ORG = organization;

            List<T_Notice> notice = NoticeService.GetTopNoticeList(6);
            ViewBag.NoticeList = notice;

            List<T_File> file = FileService.GetTopFileList(6);
            ViewBag.FileList = file;

            List<bx_Posts> bbslist = BbsService.GetPostList(6);
            ViewBag.BbsList = bbslist;

            string bbsurl = (string)System.Configuration.ConfigurationManager.AppSettings["BBS"];
            ViewBag.BbsUrl = bbsurl;
            return View(CurrUser.UserInfo);
        }
        /// <summary>
        /// 跳转BBS登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginBBS()
        {
            string bbsurl = (string)System.Configuration.ConfigurationManager.AppSettings["BBS"];
            ViewBag.BBSURL = bbsurl + "/login.aspx";
            ViewBag.UserName = CurrUser.UserInfo.User_Code;
            return View();
        }

        public ActionResult NoPermission()
        {
            return View();
        }
    }
}
